package study;

public class javastudy {
    public static void main(String[] args) {
        int[] shu = new int[]{2, 4, 5, 9, 34, 53, 992};
        daozhi(shu);
        printArray(shu);
    }

    /**
     * 遍历数组
     */
    public static void printArray(int[] h) {
        for (int i = 0; i < h.length; i++) {
            System.out.println(h[i]);
        }
    }

    /**
     * 求最大值
     */
    public static int maxValue(int[] h) {
        int result = h[0];

        for (int i = 1; i < h.length; i++) {
            if (result < h[i]) {
                result = h[i];
            }
        }

        return result;
    }

    /**
     * 冒泡排序
     */
    public static void maopao(int[] h) {
        //外层的循环是轮数
        for (int i = 0; i < h.length - 1; i++) {
            //每一轮比较的次数
            for (int j = 0; j < h.length - i - 1; j++) {
                if (h[j + 1] > h[j]) {
                    int temp = h[j + 1];
                    h[j + 1] = h[j];
                    h[j] = temp;
                }
            }
        }
        printArray(h);
    }

    /**
     * 查找
     */
    public static int findIndex(int[] h, int num) {
        int index = -1;
        for (int i = 0; i < h.length; i++) {
            if (h[i] == num) {
                //获得目标索引
                index = i;
                break;
            }
        }
        return index;
    }

    /**
     * 二分法查找
     */
    public static int findIndexBinary(int[] h, int num) {
        //最小索引
        int minIndex = 0;
        //最大索引
        int maxIndex = h.length - 1;
        //中间索引
        int middleIndex = (h.length - 1) / 2;

        while (num != h[middleIndex]) {
            //如果num大于中间索引
            if (num > h[middleIndex]) {
                minIndex = middleIndex + 1;
            } else {
                maxIndex = middleIndex - 1;
            }
            middleIndex = (maxIndex + minIndex) / 2;
        }
        return middleIndex;
    }

    /**
     * 数组倒置
     */
    public static void daozhi(int[] h){
        for (int i =0 ;i<h.length/2;i++){
            int temp = h[i];
            h[i] = h[h.length-i-1];
            h[h.length-i-1] = temp;
        }
    }
}
